const fs = require('fs')
const path = require('path')

const folder = './cards-raw'

const suitMap = {
  clubs: 'C',
  diamonds: 'D',
  hearts: 'H',
  spades: 'S'
}

const valueMap = {
  ace: 'A',
  jack: 'J',
  queen: 'Q',
  king: 'K'
}

fs.readdirSync(folder).forEach(file => {
  console.log('检测文件:', file)

  const lower = file.toLowerCase()
  if (!lower.endsWith('.svg')) {  // 这里改成 svg
    console.log('跳过非svg:', file)
    return
  }

  const originalPath = path.join(folder, file)

  if (lower.includes('joker')) {
    const newName = lower.includes('black')
      ? 'Joker_Black.svg'
      : 'Joker_Red.svg'
    const newPath = path.join(folder, newName)
    fs.renameSync(originalPath, newPath)
    console.log(`🃏 ${file} → ${newName}`)
    return
  }

  const match = lower.match(/([a-z0-9]+)_of_([a-z]+)\.svg$/)  // 正则里也改成 svg
  if (!match) {
    console.log(`⏭️ 跳过未匹配: ${file}`)
    return
  }

  const [_, valueRaw, suitRaw] = match
  const value = valueMap[valueRaw] || valueRaw.toUpperCase()
  const suit = suitMap[suitRaw]

  if (!suit) {
    console.log(`❌ 花色无法识别: ${file}`)
    return
  }

  const newName = `${value}${suit}.svg`
  const newPath = path.join(folder, newName)

  fs.renameSync(originalPath, newPath)
  console.log(`✅ ${file} → ${newName}`)
})

console.log('🚀 批量重命名完成')
